package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

// 获取指定城市的json格式的天气数据
// 101030100-天津
// http://t.weather.sojson.com/api/weather/city/101030100

type weather struct {
	Msg      string   `json:"message"`
	Status   int      `json:"status"`
	Date     string   `json:"date"`
	CityInfo CityInfo `json:"cityInfo"`
	Data     Data     `json:"data"`
}

type CityInfo struct {
	City      string `json:"city"`
	UpdatedAt string `json:"updateTime"`
}

type Data struct {
	Humidity string     `json:"shidu"`
	Pm25     float64    `json:"pm25"`
	Pm10     float64    `json:"pm10"`
	Temp     string     `json:"wendu"`
	Forecast []Forecast `json:"forecast"`
}

// "date":"01",
// "high":"高温 12℃",
// "low":"低温 1℃",
// "ymd":"2023-03-01",
// "week":"星期三",
// "sunrise":"06:43",
// "sunset":"18:03",
// "aqi":48,
// "fx":"西北风",
// "fl":"3级",
// "type":"晴",
// "notice":"愿你拥有比阳光明媚的心情"
type Forecast struct {
	Day  string `json:"date"`
	Date string `json:"ymd"`
	Week string `json:"week"`
	High string `json:"high"`
	Low  string `json:"low"`
	Stat string `json:"type"`
}

func main() {

	flag.Parse()
	NArg := flag.NArg()
	if NArg != 1 {
		fmt.Fprintln(os.Stderr, "请输入城市代码, 详见:https://github.com/baichengzhou/weather.api/blob/master/src/main/resources/citycode-2019-08-23.json")
		return
	}
	url := "http://t.weather.sojson.com/api/weather/city/" + flag.Arg(0)
	resp, err := http.Get(url)
	checkErr(err)
	data, err := ioutil.ReadAll(resp.Body)
	checkErr(err)
	weather := new(weather)
	err = json.Unmarshal(data, weather)
	checkErr(err)

	fmt.Printf("%+v", weather)

}

func checkErr(err error) {
	if err != nil {
		log.Fatalf("Error %v", err)
	}
}
